<template>
  <el-container>
    <el-header>
      <img src="@/assets/images/logo.png" alt="" />
      <el-menu
        mode="horizontal"
        background-color="#23262e"
        active-text-color="rgb(64, 158, 255)"
        text-color="#fff"
      >
        <el-submenu index="2">
          <template slot="title">
            <img class="avatar" :src="user.user_pic" alt="" v-if="user.user_pic" />
            <img class="avatar" src="@/assets/logo.png" alt="" v-else />
            个人中心</template
          >
          <el-menu-item index="2-1"><i class="el-icon-s-data"></i>用户资料</el-menu-item>
          <el-menu-item index="2-2"><i class="el-icon-s-data"></i>重置密码</el-menu-item>
          <el-menu-item index="2-2"><i class="el-icon-s-data"></i>更换头像</el-menu-item>
        </el-submenu>
        <el-menu-item index="1" @click="logout">退出</el-menu-item>
      </el-menu>
    </el-header>
    <el-container>
      <el-aside width="200px">
        <div class="user-info">
          <img class="avatar" :src="user.user_pic" alt="" v-if="user.user_pic" />
          <img class="avatar" src="@/assets/logo.png" alt="" v-else />
          <p>
            欢迎你,<span>{{ user.nickname || user.username }}</span>
          </p>
        </div>
        <el-menu
          mode="vertical"
          background-color="#23262e"
          active-text-color="rgb(64, 158, 255)"
          default-active="1"
          text-color="#fff"
          unique-opened
          router=""
        >
          <el-menu
            mode="vertical"
            background-color="#23262e"
            text-color="#fff"
            active-text-color="rgb(64, 158, 255)"
            default-active="aaa"
            unique-opened
            router=""
          >
            <template v-for="item in menu">
              <el-menu-item
                :key="item.indexPath"
                v-if="item.children === null"
                :index="item.indexPath"
              >
                <i :class="item.icon"></i>{{ item.title }}</el-menu-item
              >
              <el-submenu :key="item.indexPath" :index="item.indexPath" v-else>
                <template slot="title"><i :class="item.icon"></i>{{ item.title }}</template>
                <el-menu-item
                  :index="cldItem.indexPath"
                  v-for="cldItem in item.children"
                  :key="cldItem.indexPath"
                  ><i :class="cldItem.icon"></i>{{ cldItem.title }}</el-menu-item
                >
              </el-submenu>
            </template>
          </el-menu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import { mapState } from 'vuex'
import { getMenuAPI } from '@/api/user'
export default {
  name: 'MyHome',
  data() {
    return {
      menu: []
    }
  },

  created() {
    this.$store.dispatch('user/getUser')
    this.getMenu()
  },
  methods: {
    logout() {
      this.$confirm('你确定要退出嘛?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$router.push('/login')
        this.$store.commit('user/updateToken', '')
        this.$store.commit('user/updateUser', {})
      })
    },
    async getMenu() {
      const { data: res } = await getMenuAPI()
      // console.log(res)
      this.menu = res.data
    }
  },
  computed: {
    ...mapState('user', ['user'])
  }
}
</script>
<style lang="less" scoped>
// 头部
.el-header {
  background-color: #23262e;
  height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
// 头像
.avatar {
  height: 35px;
  width: 35px;
  border-radius: 50%;
  object-fit: cover;
  background-color: #fff;
  margin-right: 10px;
}
// 欢迎标题
.user-info {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 70px;
  p {
    color: #fff;
    font-size: 13px;
  }
}
// 侧边栏
.el-aside {
  background-color: #23262e;
  height: 100%;
  .el-menu {
    width: 200px;
    overflow: hidden;
  }
}
// 侧边栏字体
.el-menu-item {
  font-size: 13px;
}
// 下拉菜单字体
.el-submenu {
  /deep/ .el-submenu__title {
    font-size: 13px;
  }
}
// 内容区
.el-main {
  background-color: #f2f2f2;
}

.el-container {
  height: 100%;
}
</style>
